package com.xukehong.demo.util.encryption;

import java.security.MessageDigest;

/**
 * @author mm
 * Date: 2018/12/10 0010
 * Time: 17:34
 **/
public class MD5Utils {
    private static final String[] hexDigits = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};

    public MD5Utils() {
    }

    public static String MD5Encode(String data,String code) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            return byteArrayToHexString(md.digest(data.getBytes(code)));
        } catch (Exception var2) {
            var2.printStackTrace();
            return null;
        }
    }

    private static String byteArrayToHexString(byte[] b) {
        StringBuilder resultSb = new StringBuilder();
        byte[] var2 = b;
        int var3 = b.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            byte aB = var2[var4];
            resultSb.append(byteToHexString(aB));
        }

        return resultSb.toString();
    }

    private static String byteToHexString(byte b) {
        int n = b;
        if (b < 0) {
            n = b + 256;
        }

        int d1 = n >>> 4;
        int d2 = n % 16;
        return hexDigits[d1] + hexDigits[d2];
    }
}
